//
//  UIPageControlController.swift
//  HHSwift
//
//  Created by 华宏 on 2020/2/5.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit

class UIPageControlController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        title = "UIPageControl"
        
        view.addSubview(pageControl)
    }
    

    lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
        pageControl.numberOfPages = 5
        pageControl.currentPage = 0
        pageControl.hidesForSinglePage = true
        pageControl.pageIndicatorTintColor = .black
        pageControl.currentPageIndicatorTintColor = .red
        pageControl.addTarget(self, action: #selector(pageControlAction(pageControl:)), for: .valueChanged)
        
    //这个属性如果设置为YES，点击时并不会改变控制器显示的当前页码点，必须手动调用updateCurrentPageDisplay; 这个方法，才会更新。
        pageControl.defersCurrentPageDisplay = true
        pageControl.updateCurrentPageDisplay()
        
        //通过页数得到控制器大小
        let _ = pageControl.size(forNumberOfPages: 1)
        
        return pageControl
    }()
    
   @objc func pageControlAction(pageControl:UIPageControl) {
     print("currentPage:\(pageControl.currentPage)")
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if pageControl.currentPage == 4 {
            pageControl.currentPage = 0
        }else{
            pageControl.currentPage += 1
        }
    }
}
